package com.yrw.zcy.sort;

/**
 * @ClassName InsertionSort
 * @Author 12694
 * @Date 2024/11/3 20:36
 * @Version 1.0
 **/
public class InsertionSort {
    public static void main(String[] args) {
        int[] arr = {5, 2, 4, 6, 1, 3};
        insertionSort(arr);
        for (int i : arr) {
            System.out.print(i + " ");
        }
    }
    public static void insertionSort(int[] arr) {
        for (int i = 1; i < arr.length; i++) {
            for(int j=i-1;j>=0&&arr[j]>arr[j+1];j--) {
                swap(arr, j, j + 1);
            }
        }
    }
    public static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

}
